<?php

namespace App\Http\Controllers;

use App\Exceptions\ExceptionApi;
use App\Services\HttpClient;
use Illuminate\Http\Response;
use Illuminate\Http\Request;


/**
 * 足球比赛数据接口
 */
class FootballMatchController extends Controller
{
    const BASE_URL = "http://api.zaomengbao.com:89/api";

    const MATCH_CATEGORY = "/match/getCategory";
    const MATCH_REFRESH = "/match/refresh";
    // 用户关注及取消比赛 ?token=f59e0cc55ff99305c7200ca606e7b2a0dea4029c0d1c15e9d89ff29c563aa058&mid=52903653
//    const ADD_MATCH_FOLLOW = "/match/addFollow";
    // 我关注的比赛列表 ?token=f59e0cc55ff99305c7200ca606e7b2a0dea4029c0d1c15e9d89ff29c563aa058
//    const MATCH_FOLLOW = "/match/showFollow";
    const MATCH_LIST = "/match/getList";
    // 比分-即时列表
    const MATCH_LIST_BF = "/scorematch/getlist";
    // 比分-赛果列表
    const MATCH_LIST_BF_SG = "/scorematch/getEndlist";
    // 比分-赛程列表
    const MATCH_LIST_BF_SC = "/scorematch/getSchedulelist";
    // 比分-获取关注比赛id ?token=9f570e45ca9e09cded528dc5884be2dde829458bc281179ee61384838b07434a
//    const MATCH_LIST_BF_COL_IDS = "/scorematch/getFllowmatchids";

    // 比分-刷新比赛 ?ids=[53020710,53020711]
    const MATCH_LIST_BF_REFRESH = "/scorematch/refresh";
    // 比分-关注 ?page=1&token=9f570e45ca9e09cded528dc5884be2dde829458bc281179ee61384838b07434a
//    const MATCH_LIST_BF_FOLLOW = "/scorematch/showFollow";

    // 去掉竞猜guess咨询information聊天室chat tabs 比赛详情 ?id=52892610
    const MATCH_DETAIL = "/match/showinfo";
    // 开启比赛提醒闹钟 ?token=acaf5b79851915dadfc76b04babaf7e2b7503cbd19eb7a7c6679f84c482dc59f&match_id=52893690
//    const MATCH_START_TIP = "/match/opentip";
    // 获取比赛提醒闹钟状态 ?token=acaf5b79851915dadfc76b04babaf7e2b7503cbd19eb7a7c6679f84c482dc59f&match_id=52893690
//    const MATCH_START_TIP_GET = "/match/gettip";

    // 比分-筛选 ?tab=1&type=0
    const MATCH_LIST_BF_SX_LIST = "/scorematch/screen";
    // 	比分-筛选头
    const MATCH_LIST_BF_SX_HEAD = "/scorematch/screenhead";

    const MATCH_DATA_COLLECT = "/Datacollect";
    const MATCH_DATA_COLLECT_BY_URL = "/Datacollect/getDataByUrl";

    const MATCH_GET_TAB = "/scorematch/getTab";


    /**
     * @api {GET} /api/match/getCategory 获取比赛分类
     * @apiGroup 比赛数据采集
     * @apiDescription 获取比赛分类
     */
    public function getCategory(Request $request) :Response
    {
        $url = self::BASE_URL.self::MATCH_CATEGORY;
        $res = json_decode($this->getRemoteData($url, $request),true);
        foreach ($res['data'] as $key => &$item){
            if($item['league_name']=='关注'){
                unset($res['data'][$key]);
            }
        }
        $res['data'] = array_values($res['data']);
        return new Response($res);
    }

    /**
     * @api {GET} /api/match/refresh 比赛列表刷新比赛状态
     * @apiGroup 比赛数据采集
     * @apiDescription 比赛列表刷新比赛状态
     */
    public function matchRefresh(Request $request) :Response
    {
        $url = self::BASE_URL.self::MATCH_REFRESH;
        return $this->response($url, $request);
    }

    /**
     * @api {GET} /api/match/getList 获取比赛列表
     * @apiGroup 比赛数据采集
     */
    public function getList(Request $request) :Response
    {
        $url = self::BASE_URL.self::MATCH_LIST;
        $res = json_decode($this->getRemoteData($url, $request),true);
        foreach ($res['data'] as $key => &$item){
            if(strpos($item['match_title'],'NBA')!==false){
                unset($res['data'][$key]);
            }
        }
        $res['data'] = array_values($res['data']);
        return new Response($res);
    }

    /**
     * @api {GET} /api/scorematch/getlist 获取比分-即时列表
     * @apiGroup 比赛数据采集
     */
    public function getScoreList(Request $request) :Response
    {
        $url = self::BASE_URL.self::MATCH_LIST_BF;
        return $this->response($url, $request);
    }

    /**
     * @api {GET} /api/scorematch/getEndlist 比分-赛果列表
     * @apiGroup 比赛数据采集
     */
    public function getScoreEndList(Request $request) :Response
    {
        $url = self::BASE_URL.self::MATCH_LIST_BF_SG;
        return $this->response($url, $request);
    }

    /**
     * @api {GET} /api/scorematch/getSchedulelist 比分-赛程列表
     * @apiGroup 比赛数据采集
     */
    public function getScoreScheduleList(Request $request) :Response
    {
        $url = self::BASE_URL.self::MATCH_LIST_BF_SC;
        return $this->response($url, $request);
    }

    /**
     * @api {GET} /api/scorematch/refresh 比分-刷新比赛
     * @apiGroup 比赛数据采集
     */
    public function scoreRefresh(Request $request) :Response
    {
        $url = self::BASE_URL.self::MATCH_LIST_BF_REFRESH;
        return $this->response($url, $request);
    }

    /**
     * @api {GET} /api/match/showinfo 获取比赛详情
     * @apiGroup 比赛数据采集
     */
    public function getDetail(Request $request) :Response
    {
        $url = self::BASE_URL.self::MATCH_DETAIL;
        $res = json_decode($this->getRemoteData($url, $request),true);
        $tabs = [];
        foreach ($res['data'] as $key => &$value){
            if($key=='tabs'){
                // 去掉竞猜 guess 资讯 information 聊天室 chat tabs
                foreach ($value as $tab){
                    if(in_array($tab['tab'],['guess', 'information', 'chat','odds'])){
                        continue;
                    }
                    $tabs[] = $tab;
                }
            }
        }
        unset($res['data']['tabs']);
        foreach ($tabs as $tabIndex => $tab){
            if($tab['tab']=='analysis'){
                $res['data']['index'] = $tabIndex;
            }
        }
        $res['data']['tabs'] = $tabs;
        return new Response($res);
    }

    /**
     * @api {GET} /api/scorematch/screen 比分-筛选
     * @apiGroup 比赛数据采集
     */
    public function getScoreFilterList(Request $request) :Response
    {
        $url = self::BASE_URL.self::MATCH_LIST_BF_SX_LIST;
        return $this->response($url, $request);
    }

    /**
     * @api {GET} /api/scorematch/screenhead 比分-筛选头
     * @apiGroup 比赛数据采集
     */
    public function getScoreFilterHead(Request $request) :Response
    {
        $url = self::BASE_URL.self::MATCH_LIST_BF_SX_HEAD;
        return $this->response($url, $request);
    }

    /**
     * @api {GET} /api/scorematch/getTab 比分-顶部分栏
     * @apiGroup 比赛数据采集
     */
    public function getScoreTab(Request $request) :Response
    {
        $url = self::BASE_URL.self::MATCH_GET_TAB;
        $res = json_decode($this->getRemoteData($url, $request),true);
        foreach ($res['data'] as $key => &$item){
            if($item['name']=='关注'){
                unset($res['data'][$key]);
            }
        }
        $res['data'] = array_values($res['data']);
        return new Response($res);
    }

    /**
     * @api {GET} /api/Datacollect 数据抓取
     * @apiGroup 比赛数据采集
     */
    public function getData(Request $request) :Response
    {
        $url = self::BASE_URL.self::MATCH_DATA_COLLECT;
        return $this->response($url, $request);
    }

    /**
     * @api {GET} /api/Datacollect/getDataByUrl 数据抓取[通过URL]
     * @apiGroup 比赛数据采集
     */
    public function getDataByUrl(Request $request) :Response
    {
        $url = self::BASE_URL.self::MATCH_DATA_COLLECT_BY_URL;
        return $this->response($url, $request);
    }

    protected function getRemoteData($url, Request $request){
        $params = $request->all();
        if(!empty($params)){
            $queryString = http_build_query($params);
            $url = $url . "?".$queryString;
        }
        return HttpClient::get($url); // (new HttpClient($url))->getBody();
    }

    protected function response($url, Request $request){
        $data = $this->getRemoteData($url, $request);
        /*
        $data = json_decode($data,true);
        if(!isset($data['msg']) || !isset($data['code'])){
            throw new ExceptionApi('远程数据返回失败',500);
        }
        if($data['code']!=200){
            throw new ExceptionApi($data['msg'],$data['code']);
        }
        */
        return new Response($data);

    }

}
